package com.atguigu.gulimall.product.app;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * sku信息
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-08 11:28:16
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;


    //根据商品的skuId获取商品的价格
    @GetMapping("/getPrice/{skuId}")
    public BigDecimal getProductPrice(@PathVariable("skuId") Long skuId){
        SkuInfoEntity skuInfoEntity=skuInfoService.getPrice(skuId);
        return skuInfoEntity.getPrice();
    }




    /**
     * 列表
     * page: 1
     * limit: 10
     * key:
     * catelogId: 0
     * brandId: 0
     * min: 0
     * max: 0
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
//        PageUtils page = skuInfoService.queryPage(params);

        PageUtils page = skuInfoService.queryByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }


    @RequestMapping("/skuInfo/{skuId}")
    public R getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        R r = new R();
        r.setData(skuInfo);
        return r;
    }

}
